define("admin/page/role/auth", [
    "jquery",
    "vue",
    "vue-resource",
    "toastr"
], function($, Vue, VueHttp, toastr){

    Vue.use(VueHttp);

    var vm = new Vue({
        el : '#edit_auth',
        delimiters: ['${', '}'],

        data: {
            auth: [],
            token: '',
            pageData:'',
            id:''
        },
        methods: {
            submit: function(){
                var self = this;
                this.$http.post("/admin/role/auth", {
                    'id': this.id,
                    'auth': this.auth,
                    '_token': this.token
                })
                .then(function(response){
                    return response.json();
                })
                .then(function(json){
                    if (json.ret == 0) {
                        toastr.success(json.msg);
                        setTimeout(function(){
                            window.location.href = '/admin/role/list';
                        })
                    } else {
                        self.token = json.data.token;
                        toastr.warning(json.msg);
                    }
                })
                .catch(function() {
                    toastr.warning('接口请求失败');
                });
            },
            // 获取url中的参数
            getUrlParam: function(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
                var r = window.location.search.substr(1).match(reg);  //匹配目标参数
                if (r != null) return unescape(r[2]); return null; //返回参数值
            },
        },

        mounted: function(){
            Vue.set(this, 'token', $("[name='csrf-token']").attr('content'));
            Vue.set(this, 'pageData', PAGE.meta);
            this.auth = this.pageData.role_permission;
            this.id = this.getUrlParam('id');
        },
    });
});